#include <stdio.h>

void bubble_sort(int arr[], unsigned int count) {
    int swapped; // 标志变量，标记是否发生交换
    for (int i = 0; i < count - 1; i++) {
        swapped = 0; // 每轮开始时重置标志
        for (int j = 0; j < count - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                // 交换元素
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = 1; // 标记发生了交换
            }
        }
        // 如果本轮没有发生交换，说明数组已经有序，提前退出
        if (swapped == 0) {
            break;
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    unsigned int count = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    bubble_sort(arr, count);

    printf("Sorted array: ");
    for (int i = 0; i < count; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}